package com.purvar.controller;

import com.purvar.dto.Result;
import com.purvar.dto.TaskVo;
import com.purvar.sevice.LeaveFlowService;
import groovy.util.logging.Slf4j;
import org.flowable.engine.history.HistoricProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = {"http://127.0.0.1:5500/","http://localhost:5500"})
@Slf4j
public class LeaveFlowController {
    private  final Logger logger = LoggerFactory.getLogger(LeaveFlowController.class);
    @Autowired
    private LeaveFlowService flowService;

    //部署
    @GetMapping(value="/holiday/deploy")
    public Result deployProcess() {
        logger.info("leaveFlow部署---开始");
        flowService.deployProcess();
        return Result.success("leaveFlow部署---成功");
    }
    //开始
    @GetMapping(value="/holiday/start")
    public Result startProcessInstance() {
        logger.info("leaveFlow启动任务--开始");
        flowService.startProcess("leaveFlow");
        return Result.success("leaveFlow启动任务--成功");
    }
    //申请
    @GetMapping(value="/holiday/apply")
    public Result applyProcess(String name,String days) {
        logger.info("leaveFlow发起申请--开始");
        String res = flowService.applyProcess("apply",name,days);
        return Result.success(res);
    }

    //审批
    @GetMapping(value="/holiday/approve")
    public Result approveProcess( String name,String approveFlag) {
        logger.info("leaveFlow发起审批--开始");
        String res = flowService.approveProcess("approve",name,approveFlag);
        return Result.success(res);
    }

    @RequestMapping(value="/holiday/tasks", method= {RequestMethod.GET,RequestMethod.POST}, produces= MediaType.APPLICATION_JSON_VALUE)
    public List<TaskVo> getTasks() {
        logger.info("leaveFlow查询任务");
        return flowService.getTasks();
    }

    @RequestMapping(value="/holiday/historyTasks", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
    public List<TaskVo> historyTasks(){
        logger.info("leaveFlow查询历史任务");
        return flowService.historyTasks();
    }
}
